package com.test19_多线程;

/**
 * 卖票，实现一个资源共享的结构
 */
public class Demo03_Test {

    public static void main(String[] args) {
        MyThread3 myThread = new MyThread3();
        Thread threadA = new Thread(myThread);
        Thread threadB = new Thread(myThread);
        Thread threadC = new Thread(myThread);
        threadA.start();    // 启动多线程，所有的操作都针对于同一个target对象实例完成
        threadB.start();
        threadC.start();
    }
}

class MyThread3 implements Runnable {
    private int ticket = 5;  // 票数

    @Override
    public void run() {
        while (this.ticket > 0) {
            if (this.ticket > 0) {  // 判断是否有剩余的票数
                System.out.println("【卖票】ticket = " + this.ticket--);
            } else {    // 结束循环
                break;
            }
        }
    }
}
